//
//  AddContractViewController.m
//  EPBPro
//
//  Created by wucha on 2018/6/27.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "AddContractViewController.h"
#import "ResizeView.h"
#import "WCPickerView.h"
#import "HttpOrgAction.h"
#import "ContractActionInfo.h"
#import "HttpContractAction.h"
#import "EquipmentSelectViewController.h"

@interface AddContractViewController ()
@property (nonatomic,strong) NSArray *listArray;
@property (nonatomic,strong) OrgInfo *orgInfo;
@property (nonatomic,strong) UITextField *moneyTF;//金额
@property (nonatomic,strong) UITextField *equipmentTF;//设备数量
@property (nonatomic,strong) UITextField *agentAmountTF;//代理费
@property (nonatomic,strong) UITextField *returnAmountTF;//单台返利
@property (nonatomic,strong) UITextField *benifitRateTF;//利润占比
@property (nonatomic,strong) UIButton *startButton;
@property (nonatomic,strong) UIButton *endButton;
@property (nonatomic,strong) NSArray *equipmentIDs;
@property (nonatomic,strong) UILabel *totalLabel;
@end

@implementation AddContractViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = @"添加合同";
    
    ResizeView *resizeView = [[ResizeView alloc] initWithFrame:CGRectMake(0, StatusBarHeight+44, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarHeight-44)];
    [self.view addSubview:resizeView];
    
    [resizeView addResizeSubview:[self getSpaceResizeViewInfo:10]];
    ResizeViewInfo *orgTRV = [self getNormalButtonResizeViewInfo:@"机构类型:" value:@"选择类型"];
    UIButton *orgTButton = (UIButton*)[orgTRV viewWithKey:@"button"];
    [orgTButton addTarget:self action:@selector(changeOrg:) forControlEvents:UIControlEventTouchUpInside];
    [resizeView addResizeSubview:orgTRV];
    ResizeViewInfo *nameRV = [self getNormalButtonResizeViewInfo:@"公司名:" value:@"选择公司"];
    UIButton *nameButton = (UIButton*)[nameRV viewWithKey:@"button"];
    [nameButton addTarget:self action:@selector(chooseName:) forControlEvents:UIControlEventTouchUpInside];

    [resizeView addResizeSubview:nameRV];
    ResizeViewInfo *moneyResizeViewInfo = [self getNormalResizeViewInfo:@"金额:" value:@""];
    self.moneyTF = (UITextField*)[moneyResizeViewInfo viewWithKey:@"TF"];
    _moneyTF.placeholder = @"单位(元)";
    _moneyTF.keyboardType = UIKeyboardTypeDecimalPad;
    [resizeView addResizeSubview:moneyResizeViewInfo];
    
    
    [resizeView addResizeSubview:[self getEquipmentListResizeViewInfo:@"设备选择"]];
    
    ResizeViewInfo *agentAmountResizeViewInfo = [self getNormalResizeViewInfo:@"代理费:" value:@""];
    self.agentAmountTF = (UITextField*)[agentAmountResizeViewInfo viewWithKey:@"TF"];
    _agentAmountTF.placeholder = @"单位(元)";
    [resizeView addResizeSubview:agentAmountResizeViewInfo];
    ResizeViewInfo *returnAmountResizeViewInfo = [self getNormalResizeViewInfo:@"单台落地返利:" value:@""];
    self.returnAmountTF = (UITextField*)[returnAmountResizeViewInfo viewWithKey:@"TF"];
    _returnAmountTF.placeholder = @"单位(元)";
    [resizeView addResizeSubview:returnAmountResizeViewInfo];
    
    ResizeViewInfo *benifitRateResizeViewInfo = [self getNormalResizeViewInfo:@"利润占比:" value:@""];
    self.benifitRateTF = (UITextField*)[benifitRateResizeViewInfo viewWithKey:@"TF"];
    _benifitRateTF.placeholder = @"单位(%)";
    [resizeView addResizeSubview:benifitRateResizeViewInfo];
    ResizeViewInfo *startResizeViewInfo = [self getNormalButtonResizeViewInfo:@"开始时间:" value:@"请选择开始时间"];
    UIButton *startButton = (UIButton*)[startResizeViewInfo viewWithKey:@"button"];
    [startButton addTarget:self action:@selector(startButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.startButton = startButton;
    [resizeView addResizeSubview:startResizeViewInfo];
    
    ResizeViewInfo *endResizeViewInfo = [self getNormalButtonResizeViewInfo:@"结束时间:" value:@"请选择结束时间"];
    UIButton *endButton = (UIButton*)[endResizeViewInfo viewWithKey:@"button"];
    [endButton addTarget:self action:@selector(endButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.endButton = endButton;
    [resizeView addResizeSubview:endResizeViewInfo];
    [resizeView addResizeSubview:[self getSpaceResizeViewInfo:20]];
    [resizeView addResizeSubview:[self getSubmitResizeViewInfo]];

}

-(ResizeViewInfo*)getEquipmentListResizeViewInfo:(NSString*)title{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(100);
    
    UIImageView *arrowImageView = [UIImageView new];
    arrowImageView.image = [UIImage imageNamed:@"arrow_right"];
    arrowImageView.contentMode = UIViewContentModeCenter;
    [resizeViewInfo addSubview:arrowImageView];
    arrowImageView.sd_layout.rightSpaceToView(resizeViewInfo.view, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(20);
    
    UIButton *button = [UIButton new];
    [button addTarget:self action:@selector(equipmentListAction:) forControlEvents:UIControlEventTouchUpInside];
    [resizeViewInfo addSubview:button];
    button.sd_layout.leftSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 0).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0);
    
    UILabel *totalLabel = [UILabel new];
    totalLabel.textAlignment = NSTextAlignmentRight;
    totalLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:totalLabel];
    self.totalLabel = totalLabel;
    totalLabel.sd_layout.rightSpaceToView(arrowImageView, 5).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(100);
    
    return resizeViewInfo;
}

-(void)equipmentListAction:(id)sender{
    NSString *type;
    if ([self.orgInfo.orgType isEqualToString:@"1"]) {
        type = @"AGENT";
        
    }else if ([self.orgInfo.orgType isEqualToString:@"2"]){
        type = @"CHANNEL";
        
    }else if ([self.orgInfo.orgType isEqualToString:@"3"]){
        type = @"RENT";
        
    }
    EquipmentSelectViewController *equipmentSelectViewController = [[EquipmentSelectViewController alloc] initWithOrgType:type Block:^(NSArray *array){
        self.equipmentIDs = array;
        self.totalLabel.text = [NSString stringWithFormat:@"共选择%ld台",array.count];
    }];
    [self.navigationController pushViewController:equipmentSelectViewController animated:YES];
}

-(void)startButtonAction:(id)sender{
    [self.view endEditing:YES];
    UIButton *button = sender;
    WCPickerView *wcPickerView = [[WCPickerView alloc] initWithDataPick];
    [wcPickerView setWcDataPickSuccessWithBlock:^(NSString *string) {
        [button setTitle:string forState:UIControlStateNormal];
    }];
    [self.view addSubview:wcPickerView];

}

-(void)endButtonAction:(id)sender{
    [self.view endEditing:YES];
    UIButton *button = sender;
    WCPickerView *wcPickerView = [[WCPickerView alloc] initWithDataPick];
    [wcPickerView setWcDataPickSuccessWithBlock:^(NSString *string) {
        [button setTitle:string forState:UIControlStateNormal];
    }];
    [self.view addSubview:wcPickerView];
}

-(void)changeOrg:(id)sender{
    UIButton *button = sender;
    [self.view endEditing:YES];
    NSArray *array = @[@"代理商",@"渠道商",@"广告商"];
    WCPickerView *wcPickerView = [[WCPickerView alloc] initViewWithArray:array];
    [wcPickerView setWcPickSuccessWithBlock:^(NSInteger result) {
        [button setTitle:array[result] forState:UIControlStateNormal];
        [self loadData:(int)result+1];
    }];
    [self.view addSubview:wcPickerView];
}

-(void)chooseName:(id)sender{
    UIButton *button = sender;
    [self.view endEditing:YES];
    if (self.listArray.count == 0) {
        [self showMessage:@"请先添加机构" autoDissmissAfterDelay:0];
        return;
    }
    NSMutableArray *nameMutableArray = [NSMutableArray array];
    for (OrgInfo *orgInfo in self.listArray) {
        [nameMutableArray addObject:orgInfo.name];
    }
    WCPickerView *wcPickerView = [[WCPickerView alloc] initViewWithArray:nameMutableArray];
    [wcPickerView setWcPickSuccessWithBlock:^(NSInteger result) {
        [button setTitle:nameMutableArray[result] forState:UIControlStateNormal];
        self.orgInfo = self.listArray[result];
        
    }];
    [self.view addSubview:wcPickerView];
}

-(void)loadData:(int)tag{
    [self showWaitingViewWithMessage:@"加载中..." style:WaitingViewShowStyleFullSelfView];
    OrgActionInfo *orgActionInfo = [[OrgActionInfo alloc] init];
    orgActionInfo.orgType = [NSString stringWithFormat:@"%d",tag];
    NSDictionary *dic = [Utils objectToDictionary:orgActionInfo];
    [HttpOrgAction postSearchOrgList:dic block:^(id result, NSError *error) {
        [self dismissWaitingView];
        if (!error) {
            self.listArray = result;
            
        }
    }];
}

-(ResizeViewInfo*)getNormalResizeViewInfo:(NSString*)title value:(NSString*)placeHolder{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UITextField *nameTF = [UITextField new];
    nameTF.textColor = [UIColor grayColor];
    nameTF.placeholder = placeHolder;
    nameTF.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:nameTF viewKey:@"TF"];
    nameTF.sd_layout.leftSpaceToView(titleLabel, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(150);
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getNormalButtonResizeViewInfo:(NSString*)title value:(NSString*) valueString{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UIButton *button = [UIButton new];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button setTitle:valueString forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [resizeViewInfo addSubview:button viewKey:@"button"];
    button.sd_layout.leftSpaceToView(titleLabel, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(150);
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getSpaceResizeViewInfo:(int)height{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = height;
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getSubmitResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 40;
    
    UIButton *submitButton = [UIButton new];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [submitButton setTitle:@"提交" forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitButton.backgroundColor = DefaultTenantColor;
    [resizeViewInfo addSubview:submitButton];
    submitButton.sd_layout.leftSpaceToView(resizeViewInfo.view, 40).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(SCREEN_WIDTH-80);
    
    return resizeViewInfo;
}

-(void)submitAction:(id)sender{
    if (self.orgInfo == nil) {
        [self showMessage:@"请添加机构" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.moneyTF.text isEqualToString:@""]) {
        [self showMessage:@"请填写金额" autoDissmissAfterDelay:0];
        return;
    }
//    if ([self.equipmentTF.text isEqualToString:@""]) {
//        [self showMessage:@"请填写设备数量" autoDissmissAfterDelay:0];
//        return;
//    }
    if (self.equipmentIDs.count==0) {
        [self showMessage:@"请选择设备" autoDissmissAfterDelay:0];

        return;
    }
    if ([self.agentAmountTF.text isEqualToString:@""]) {
        [self showMessage:@"请填写代理费" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.returnAmountTF.text isEqualToString:@""]) {
        [self showMessage:@"请填写单台返利" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.benifitRateTF.text isEqualToString:@""]) {
        [self showMessage:@"请填写利润占比" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.startButton.titleLabel.text isEqualToString:@"请选择开始时间"]) {
        [self showMessage:@"请选择开始时间" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.startButton.titleLabel.text isEqualToString:@"请选择结束时间"]) {
        [self showMessage:@"请选择结束时间" autoDissmissAfterDelay:0];
        return;
    }
//    NSArray *equipmentID = @[@"1",@"2"];
    AddContractActionInfo *contractActionInfo = [[AddContractActionInfo alloc] init];
    contractActionInfo.yiOrgId = self.orgInfo.id;
    contractActionInfo.province = self.orgInfo.province?self.orgInfo.province:@"江苏";
    contractActionInfo.city = self.orgInfo.city;
    contractActionInfo.area = self.orgInfo.area;
    contractActionInfo.contactor = self.orgInfo.contractor;
    contractActionInfo.contactPhone = self.orgInfo.contractPhone;
    contractActionInfo.amount = self.moneyTF.text;
    contractActionInfo.equipmentNum = [NSString stringWithFormat:@"%ld",self.equipmentIDs.count];
    contractActionInfo.agentAmount = self.agentAmountTF.text;
    contractActionInfo.returnAmount = self.returnAmountTF.text;
    contractActionInfo.benifitRate = [NSString stringWithFormat:@"%.2f",[self.benifitRateTF.text floatValue]/100.0f];
    contractActionInfo.startDate = [NSString stringWithFormat:@"%@ 00:00:00",self.startButton.titleLabel.text];
    contractActionInfo.endDate = [NSString stringWithFormat:@"%@ 00:00:00",self.endButton.titleLabel.text];
    contractActionInfo.equipmentIds = self.equipmentIDs;
    if ([self.orgInfo.orgType isEqualToString:@"1"]) {
        contractActionInfo.type = @"AGENT";

    }else if ([self.orgInfo.orgType isEqualToString:@"2"]){
        contractActionInfo.type = @"CHANNEL";

    }else if ([self.orgInfo.orgType isEqualToString:@"3"]){
        contractActionInfo.type = @"RENT";

    }
    [self showWaitingViewWithMessage:@"加载中..." style:WaitingViewShowStyleFullSelfView];
    [HttpContractAction postAddContractWithAction:contractActionInfo block:^(id result, NSError *error) {
        [self dismissWaitingView];
        if (!error) {
            [self showMessage:@"添加成功" autoDissmissAfterDelay:0];
            [self performBlock:^{
                [self.navigationController popViewControllerAnimated:YES];
            } afterDelay:0.8];
        }
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
